<template>
  <div class="login-container">
    <!-- 左侧图片展示区 -->
    <div class="login-left">
      <div class="login-banner">
        <img src="@/assets/login-banner.png" alt="登录banner" class="banner-img">
        <div class="banner-text">
          <h2>欢迎使用OA系统</h2>
          <p>高效、便捷的办公自动化解决方案</p>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单区 -->
    <div class="login-right">
      <div class="login-form-container">
        <div class="login-header">
          <h2 class="welcome-text">欢迎登录</h2>
          <p class="sub-title">请使用您的账号密码登录系统</p>
        </div>

        <!-- 登录方式切换 -->
        <div class="login-type-switch">
          <span 
            :class="['switch-item', activeType === 'account' ? 'active' : '']" 
            @click="activeType = 'account'"
          >账号密码登录</span>
          <span 
            :class="['switch-item', activeType === 'phone' ? 'active' : '']" 
            @click="activeType = 'phone'"
          >手机号登录</span>
        </div>

        <!-- 账号密码登录 -->
        <el-form
          v-if="activeType === 'account'"
          ref="loginForm"
          :model="form"
          :rules="loginRules"
          class="login-account-form"
        >
          <el-form-item prop="username">
            <div class="custom-input">
              <el-input
                v-model="form.username"
                type="text"
                auto-complete="off"
                placeholder="请输入用户名"
              >
                <template #prefix>
                  <el-icon class="input-icon"><User /></el-icon>
                </template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item prop="password">
            <div class="custom-input">
              <el-input
                v-model="form.password"
                type="password"
                auto-complete="off"
                show-password
                placeholder="请输入密码"
              >
                <template #prefix>
                  <el-icon class="input-icon"><Key /></el-icon>
                </template>
              </el-input>
            </div>
          </el-form-item>
          <div class="remember-forgot">
            <el-checkbox v-model="rememberMe">记住密码</el-checkbox>
            <a href="javascript:;" class="forgot-link" @click="handleForgotPassword">忘记密码？</a>
          </div>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              class="login-btn"
              @click="submit"
            >登录</el-button>
          </el-form-item>
        </el-form>

        <!-- 手机号登录 -->
        <el-form
          v-else
          ref="phoneForm"
          :model="phoneFormData"
          :rules="phoneRules"
          class="login-phone-form"
        >
          <el-form-item prop="phone">
            <div class="custom-input">
              <el-input
                v-model="phoneFormData.phone"
                placeholder="请输入手机号"
                maxlength="11"
              >
                <template #prefix>
                  <el-icon class="input-icon"><Iphone /></el-icon>
                </template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item prop="code">
            <div class="verify-code-input">
              <div class="custom-input code-input">
                <el-input
                  v-model="phoneFormData.code"
                  placeholder="请输入验证码"
                  maxlength="6"
                >
                  <template #prefix>
                    <el-icon class="input-icon"><Message /></el-icon>
                  </template>
                </el-input>
              </div>
              <el-button 
                type="primary" 
                class="send-code-btn"
                :disabled="cooldown > 0"
                @click="sendCode"
              >{{ cooldown > 0 ? `${cooldown}s` : '发送验证码' }}</el-button>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button 
              type="primary" 
              class="login-btn"
              :loading="loading" 
              @click="phoneLogin"
            >登录</el-button>
          </el-form-item>
        </el-form>

        <!-- 第三方登录 -->
        <div class="other-login">
          <div class="divider">
            <span>其他登录方式</span>
          </div>
          <div class="other-login-btns">
            <el-tooltip content="微信登录" placement="top">
              <el-button class="social-btn" circle>
                <svg-icon icon-class="wechat" class="social-icon" />
              </el-button>
            </el-tooltip>
            <el-tooltip content="QQ登录" placement="top">
              <el-button class="social-btn" circle>
                <svg-icon icon-class="qq" class="social-icon" />
              </el-button>
            </el-tooltip>
            <el-tooltip content="钉钉登录" placement="top">
              <el-button class="social-btn" circle>
                <svg-icon icon-class="dingtalk" class="social-icon" />
              </el-button>
            </el-tooltip>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, toRefs, ref } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { login } from "@/api/index";
import { User, Key, Iphone, Message } from '@element-plus/icons-vue'

const activeType = ref('account');
const rememberMe = ref(false);
const cooldown = ref(0);
const loading = ref(false);
const loginForm = ref(null);
const phoneForm = ref(null);

// 账号密码登录表单
const form = reactive({
  username: 'admin',
  password: '123456',
});

// 手机号登录表单
const phoneFormData = reactive({
  phone: '',
  code: ''
});

// 账号密码登录验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
};

// 手机号登录验证规则
const phoneRules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 6, message: '验证码长度应为6位', trigger: 'blur' }
  ]
};

const redirect = ref(undefined);
const store = useStore();
const router = useRouter();

// 账号密码登录提交
const submit = async () => {
  if (!loginForm.value) return;
  
  try {
    await loginForm.value.validate();
    loading.value = true;
    await store.dispatch("user/login", form);
    router.push({ path: redirect.value || "/" });
  } catch (error) {
    console.error('登录验证失败:', error);
  } finally {
    loading.value = false;
  }
};

// 发送验证码
const sendCode = async () => {
  if (!phoneForm.value) return;
  
  try {
    // 验证手机号
    await phoneForm.value.validateField('phone');
    
    // 开始倒计时
    cooldown.value = 60;
    const timer = setInterval(() => {
      cooldown.value--;
      if (cooldown.value <= 0) {
        clearInterval(timer);
      }
    }, 1000);

    // TODO: 调用发送验证码接口
    console.log('发送验证码到手机号:', phoneFormData.phone);
    
  } catch (error) {
    console.error('手机号验证失败:', error);
  }
};

// 手机号登录
const phoneLogin = async () => {
  if (!phoneForm.value) return;
  
  try {
    await phoneForm.value.validate();
    loading.value = true;
    // TODO: 实现手机号登录逻辑
    console.log('手机号登录:', phoneFormData);
  } catch (error) {
    console.error('手机号登录验证失败:', error);
  } finally {
    loading.value = false;
  }
};

// 忘记密码
const handleForgotPassword = () => {
  // TODO: 实现忘记密码逻辑
  console.log('忘记密码');
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  width: 100%;
  height: 100vh;
  background: #f5f7fa;
  overflow: hidden;
}

.login-left {
  flex: 1;
  position: relative;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;

  .login-banner {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #fff;
    text-align: center;
    padding: 40px;

    .banner-img {
      max-width: 80%;
      margin-bottom: 40px;
    }

    .banner-text {
      h2 {
        font-size: 36px;
        margin-bottom: 20px;
        font-weight: 600;
      }

      p {
        font-size: 18px;
        opacity: 0.8;
      }
    }
  }
}

.login-right {
  width: 500px;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.05);

  .login-form-container {
    width: 100%;
    max-width: 360px;
  }

  .login-header {
    text-align: center;
    margin-bottom: 40px;

    .welcome-text {
      font-size: 28px;
      color: #333;
      margin-bottom: 12px;
      font-weight: 600;
    }

    .sub-title {
      font-size: 14px;
      color: #666;
    }
  }
}

.login-type-switch {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
  border-bottom: 1px solid #eee;
  padding-bottom: 20px;

  .switch-item {
    position: relative;
    padding: 0 20px;
    font-size: 16px;
    color: #666;
    cursor: pointer;
    transition: all 0.3s;

    &.active {
      color: var(--el-color-primary);
      font-weight: 500;

      &::after {
        content: '';
        position: absolute;
        bottom: -21px;
        left: 50%;
        transform: translateX(-50%);
        width: 20px;
        height: 2px;
        background: var(--el-color-primary);
      }
    }
  }
}

.custom-input {
  width: 100%;
  
  :deep(.el-input__wrapper) {
    padding: 0 15px;
    height: 44px;
    box-shadow: 0 0 0 1px #dcdfe6 inset;
    transition: all 0.3s;
    width: 100%;

    &:hover {
      box-shadow: 0 0 0 1px var(--el-color-primary) inset;
    }

    &.is-focus {
      box-shadow: 0 0 0 1px var(--el-color-primary) inset;
    }
  }

  :deep(.el-input__inner) {
    height: 42px;
    line-height: 42px;
  }

  .input-icon {
    font-size: 18px;
    color: #999;
  }
}

.verify-code-input {
  display: flex;
  gap: 12px;
  width: 100%;

  .code-input {
    flex: 1;
    width: 0; // 确保flex布局下宽度正确
  }

  .send-code-btn {
    width: 120px;
    height: 44px;
    flex-shrink: 0; // 防止按钮被压缩
  }
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;

  .forgot-link {
    color: var(--el-color-primary);
    text-decoration: none;
    font-size: 14px;

    &:hover {
      text-decoration: underline;
    }
  }
}

.login-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
}

.other-login {
  margin-top: 40px;

  .divider {
    position: relative;
    text-align: center;
    margin-bottom: 24px;

    &::before,
    &::after {
      content: '';
      position: absolute;
      top: 50%;
      width: 30%;
      height: 1px;
      background: #eee;
    }

    &::before {
      left: 0;
    }

    &::after {
      right: 0;
    }

    span {
      display: inline-block;
      padding: 0 12px;
      color: #999;
      background: #fff;
      font-size: 14px;
    }
  }

  .other-login-btns {
    display: flex;
    justify-content: center;
    gap: 24px;

    .social-btn {
      width: 40px;
      height: 40px;
      padding: 0;
      border: 1px solid #eee;
      background: #fff;
      transition: all 0.3s;

      &:hover {
        border-color: var(--el-color-primary);
        color: var(--el-color-primary);
      }

      .social-icon {
        font-size: 20px;
      }
    }
  }
}

.login-account-form,
.login-phone-form {
  width: 100%;
  
  :deep(.el-form-item) {
    width: 100%;
    margin-bottom: 24px;
  }
}

// 响应式设计
@media screen and (max-width: 1200px) {
  .login-left {
    display: none;
  }

  .login-right {
    width: 100%;
  }
}
</style>